package com.zw.boot304web.controller;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.dataformat.yaml.YAMLFactory;
import com.zw.boot304web.pojo.Person;
import jakarta.servlet.http.HttpServletRequest;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;

@Slf4j
@RestController
public class HelloController {

    /**
     * 默认使用新版（Spring5之后）PathPatternParser 进行路径匹配
     * 不能匹配 ** 在中间的情况，剩下的和 antPathMatcher语法相同
     *
     * @param request
     * @param p1
     * @return string
     */
    @GetMapping("/a*/b?/{p1:[a-f]+}/**")
    public String hello(HttpServletRequest request, @PathVariable String p1) {
        String uri = request.getRequestURI();
        log.info("p1：{}", p1);

        return uri;
    }

    /**
     * 1. 默认支持把 对象转换成 json，因为 默认 Web场景导入了 jackson处理json的包
     * 2. jackson 也支持把数据写为 xml，但需要导入 jackson-dataformat-xml 包
     *
     * @return com.zw.boot304web.pojo.Person
     * @author 海棠
     */
    @GetMapping("/person")
    public Person person() {
        Person person = new Person();
        person.setName("zhangsan");
        person.setAge(18);
        person.setAddress("beijing");
        person.setEmail("zhangsan@163.com");

        return person;
    }

    public static void aaa(String[] args) {
        ObjectMapper objectMapper = new ObjectMapper(new YAMLFactory());
        Person person = new Person();
        person.setName("zhangsan");
        person.setAge(18);
        person.setAddress("beijing");
        person.setEmail("zhangsan@163.com");
        try {
            String yaml = objectMapper.writeValueAsString(person);
            System.out.println(yaml);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
